package com.sxg.controller;

import com.sxg.annotation.Anonymous;
import com.sxg.config.RabbitMqConfig;
import com.sxg.convert.SxgCategoryConvert;
import com.sxg.entity.SxgCategoryEntity;
import com.sxg.query.SxgCategoryQuery;
import com.sxg.utils.code.AjaxResult;
import com.sxg.vo.SxgCategoryVO;
import com.sxg.vo.resp.CategoryRespVo;
import lombok.AllArgsConstructor;
import com.sxg.page.PageResult;
import com.sxg.utils.code.Result;
import com.sxg.service.SxgCategoryService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import javax.annotation.Resource;
import java.util.List;

/**
* 商品分类
*
* @author 浪漫写尽歌谱 3298751420@qq.com
* @since 1.0.0 2024-03-07
*/
@RestController
@RequestMapping("sxg/sxg_category")
@AllArgsConstructor
public class SxgCategoryController {
    private final SxgCategoryService sxgCategoryService;

    @GetMapping("page")
    @PreAuthorize("hasAuthority('sxg:sxg_category:page')")
    @Anonymous
    public Result<PageResult<SxgCategoryVO>> page(@Valid SxgCategoryQuery query){
        PageResult<SxgCategoryVO> page = sxgCategoryService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('sxg:sxg_category:info')")
    public Result<SxgCategoryVO> get(@PathVariable("id") Long id){
        SxgCategoryEntity entity = sxgCategoryService.getById(id);

        return Result.ok(SxgCategoryConvert.INSTANCE.convert(entity));
    }

    @GetMapping("list")
    @Anonymous
    @Cacheable(cacheNames = "sxg:category",key = "'list'")
    public AjaxResult list(){
        List<CategoryRespVo> list = sxgCategoryService.selectList();
        return AjaxResult.success(list);
    }

    @PostMapping
    @PreAuthorize("hasAuthority('sxg:sxg_category:save')")
    public Result<String> save(@RequestBody SxgCategoryVO vo){
        sxgCategoryService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @PreAuthorize("hasAuthority('sxg:sxg_category:update')")
    public Result<String> update(@RequestBody @Valid SxgCategoryVO vo){
        sxgCategoryService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @PreAuthorize("hasAuthority('sxg:sxg_category:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        sxgCategoryService.delete(idList);

        return Result.ok();
    }
}